
#include <QDebug>
#include <QPlainTextEdit>
#include <QPushButton>
#include <QTabWidget>

#include "flowlayout.h"
#include "homepage.h"
#include "picturecaptionwidget.h"
#include "tooltype.h"

HomePage::HomePage(QWidget *parent)
    : QWidget(parent)
{
    FlowLayout *flowLayout = new FlowLayout;

    QPixmap imageHex2Char(":/icon/hex2char.png");
    PictureCaptionWidget *captionHex2Char = new PictureCaptionWidget(ToolType::TOOL_HEX_CONVERTER,
                                                                     imageHex2Char,
                                                                     tr("十六进制转换"),
                                                                     tr("Hex与字符互转"),
                                                                     this);
    flowLayout->addWidget(captionHex2Char);

    QPixmap imageSerialPort(":/icon/SerialPort.png");
    PictureCaptionWidget *captionSerialPort
        = new PictureCaptionWidget(ToolType::TOOL_SERIAL_PORT_TOOL,
                                   imageSerialPort,
                                   tr("串口调试助手"),
                                   tr("收发串口数据"),
                                   this);
    flowLayout->addWidget(captionSerialPort);

    QPixmap imageTCPClient(":/icon/TCP客户端.png");
    PictureCaptionWidget *captionTCPClient = new PictureCaptionWidget(ToolType::TOOL_TCP_CLIENT,
                                                                      imageTCPClient,
                                                                      tr("TCP客户端"),
                                                                      tr("发起TCP连接并收发数据"),
                                                                      this);
    flowLayout->addWidget(captionTCPClient);

    QPixmap imageTCPServer(":/icon/TCP服务端.png");
    PictureCaptionWidget *captionTCPServer = new PictureCaptionWidget(ToolType::TOOL_TCP_SERVER,
                                                                      imageTCPServer,
                                                                      tr("TCP服务端"),
                                                                      tr("接受TCP连接并收发数据"),
                                                                      this);
    flowLayout->addWidget(captionTCPServer);

    QPixmap imageUDPClient(":/icon/UDP客户端.png");
    PictureCaptionWidget *captionUDPClient = new PictureCaptionWidget(ToolType::TOOL_UDP_CLIENT,
                                                                      imageUDPClient,
                                                                      tr("UDP客户端"),
                                                                      tr("收发UDP数据"),
                                                                      this);
    flowLayout->addWidget(captionUDPClient);

    QPixmap imageUDPServer(":/icon/UDP服务端.png");
    PictureCaptionWidget *captionUDPServer = new PictureCaptionWidget(ToolType::TOOL_UDP_SERVER,
                                                                      imageUDPServer,
                                                                      tr("UDP服务端"),
                                                                      tr("收发UDP数据"),
                                                                      this);
    flowLayout->addWidget(captionUDPServer);

    QPixmap imageNanZi103Server(":/icon/南自103服务端.png");
    PictureCaptionWidget *captionNanZi103Server
        = new PictureCaptionWidget(ToolType::TOOL_NANZI103_SERVER,
                                   imageNanZi103Server,
                                   tr("南自103服务端"),
                                   tr("南自103服务端"),
                                   this);
    flowLayout->addWidget(captionNanZi103Server);

    QPixmap imageNanZi103Client(":/icon/南自103模拟器.png");
    PictureCaptionWidget *captionNanZi103Client
        = new PictureCaptionWidget(ToolType::TOOL_NANZI103_CLIENT,
                                   imageNanZi103Client,
                                   tr("南自103模拟器"),
                                   tr("南自103模拟器"),
                                   this);
    flowLayout->addWidget(captionNanZi103Client);

    QPixmap imageNanZi103(":/icon/南自103.png");
    PictureCaptionWidget *captionNanZi103
        = new PictureCaptionWidget(ToolType::TOOL_NANZI103_PROTOCOL_PARSER,
                                   imageNanZi103,
                                   tr("南自以太网103规约"),
                                   tr("南自以太网103规约解析器"),
                                   this);
    flowLayout->addWidget(captionNanZi103);

    QPixmap imageSerialPort103(":/icon/串口103.png");
    PictureCaptionWidget *captionSerialPort103
        = new PictureCaptionWidget(ToolType::TOOL_SERIALPORT103_PROTOCOL_PARSER,
                                   imageSerialPort103,
                                   tr("串口103规约"),
                                   tr("串口103规约解析器"),
                                   this);

    flowLayout->addWidget(captionSerialPort103);

    auto children = this->children();
    for (const auto &child : children) {
        PictureCaptionWidget *caption = qobject_cast<PictureCaptionWidget *>(child);
        if (caption != nullptr) {
            connect(caption, &PictureCaptionWidget::clicked, this, &HomePage::captionClicked);
        }
    }

    setLayout(flowLayout);
    setWindowTitle(tr("主页"));
    setWindowIcon(QIcon(":/icon/主页.png"));
}
